package com.itheima.service.impl;

import com.itheima.dao.OrderSettingMapper;
import com.itheima.pojo.OrderSetting;
import com.itheima.pojo.OrderSettingEdit;
import com.itheima.service.OrderSettingService;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.time.LocalDate;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingMapper orderSettingMapper;

    // 指定文件存储的目录
    private static final String FILE_STORAGE_DIR = "D:\\code\\bxg-health-parent\\bxg-health-backend\\src\\main\\java\\com\\itheima\\template";

    /**
     * 文件下载
     * @param filename
     * @param response
     */
    public void download(String filename, HttpServletResponse response) {
        // 定义基础目录
        String baseDir = "D:\\code\\bxg-health-parent\\bxg-health-backend\\src\\main\\java\\com\\itheima\\template";
        // 拼接完整路径
        String filePath = baseDir + File.separator + filename;

        // 检查文件是否存在
        File file = new File(filePath);
        if (!file.exists()) {
            throw new RuntimeException("文件未找到: " + filePath);
        }

        try (InputStream in = new FileInputStream(file);
             ServletOutputStream out = response.getOutputStream()) {

            // 加载 Excel 文件
            XSSFWorkbook excel = new XSSFWorkbook(in);

            // 设置响应头
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(filename, "UTF-8"));

            // 输出到客户端
            excel.write(out);
            out.flush();

        } catch (IOException e) {
            throw new RuntimeException("文件下载失败: " + e.getMessage(), e);
        }
    }

    /**
     * 修改预约设置
     * @param orderSettingEdit
     */
    public void editNumberByOrderDate(OrderSettingEdit orderSettingEdit) {


        orderSettingMapper.editNumberByOrderDate(orderSettingEdit);
    }


    @Override
    public List<OrderSetting> getOrderSettingByMonth(String date) {
        // 定义日期时间格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-M");
        // 解析字符串为 YearMonth 对象
        YearMonth yearMonth = YearMonth.parse(date, formatter);

        // 获取该月最早的一天
        LocalDate firstDayOfMonth = yearMonth.atDay(1);

        // 获取该月最晚的一天
        LocalDate lastDayOfMonth = yearMonth.atEndOfMonth();

        Map map = new HashMap();
        map.put("firstDayOfMonth", firstDayOfMonth);
        map.put("lastDayOfMonth", lastDayOfMonth);
        orderSettingMapper.getOrderSettingByMonth(map);
        return null;
    }

    /**
     * 上传excel文件
     * @param file
     */
    public void upload(MultipartFile file) {
        String filePath = FILE_STORAGE_DIR + File.separator + file.getOriginalFilename();
        try {
            file.transferTo(new File(filePath));
        } catch (IOException e) {
            throw new RuntimeException("文件上传失败: " + e.getMessage(), e);
        }
    }
}
